package com.interview.javabasic.thread;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo implements Runnable {
    private static ReentrantLock lock = new ReentrantLock(true);//公平锁
    @Override
    public void run() {
        while(true){
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"get lock.");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
        }

    public static void main(String[] args) {
        ReentrantLockDemo rtld = new ReentrantLockDemo();
        Thread t1 =new Thread(rtld);
        Thread t2 =  new Thread(rtld);
        t1.start();
        t2.start();
    }
}
